package cn.edu.glut.utils;

import org.springframework.context.annotation.Configuration;

import cn.edu.glut.user.service.UserDetailService;

/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月27日下午6:35:03
 * @email Caizhfy@163.com
 * @description 简易权限管理
 * 策略：角色对资源
 *
 */
@Configuration
public class AccessDecisionManager {
	//单下划线代表路径会带参数
	private static String[] studentResources = { "/selection/list/stu", "/selection/refund/_",
			"/subject/list", "/selection/teach/choice"};

	private static String[] teacherResources = { "/subject/list", "/subject/new", "/subject/del/_", "/subject/update",
			"/stu/sub/list/{subjectId}" };

	private static String[] adminResources = { "/stu/new/upload", "/stu/new", "/stu/update", "/stu/get/_",
			"/stu/get/list", "/stu/del/_", "/stu/dels", "/stu/get/um/_", "/selection/list/stu", "/teac/new",
			"/teac/update", "/teac/del/_", "/teac/dels", "/teac/get/um/_", "/teac/list", "/teac/new/upload",
			"/subject/list", "/subject/list", "/stu/sub/list/_", "/selection/teach/choice" };

//	private static String[] publicResources = {};
	
	public boolean decide(Integer userType, String uri) {
		/*if(match(uri, AccessDecisionManager.publicResources))
			return true;*/
		if(userType == UserDetailService.TYPE_STUDENT)
			return match(uri, AccessDecisionManager.studentResources);
		if(userType == UserDetailService.TYPE_ADMIN)
			return match(uri, AccessDecisionManager.adminResources);
		if(userType == UserDetailService.TYPE_TEACHER)
			return match(uri, AccessDecisionManager.teacherResources);
		return false;
	}
	
	
	//匹配uri与该资源集合中是否包含
	private boolean match(String uri, String[] resources){
		for(String resource : resources){
			//当且仅当uri包含该资源的时候，才能放行，否则都是没有权限
			if(uri.contains(resource))
				return true;
		}
		return false;
	}
	
}
